import PAGE from "../../../utils/middleware/page";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import store from "../../../store/index";
const app = getApp();
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh'
        }
    },
    data: {
        loaded: false,
        list: [],
        listField: {
            module: "member",
            api: "Search",
            data: "list"
        },
        searchParams: {
            PageIndex: 1,
            PageSize: 10,
            MemberPhone: null
        },
    },
    onLoad() { },
    init() {
        this.data.searchParams.PageIndex = 1;
        this.fetchList();
    },
    async fetchList() {
        this.handleFetchListData({
            complete: (data) => { },
            error: (data) => {
                wx.$Fun.showToast(data.Message);
            }
        }, 'listField', this.data.searchParams.PageIndex == 1)
    },
    handleSearch(e) {
        if (e.detail) {
            this.data.searchParams.MemberPhone = e.detail;
            this.init({});
        }
    },
    handleClear(e) {
        this.setData({
            loaded: false,
            list: [],
        });
    },
    handleCancel(e) {
        this.setData({
            loaded: false,
            list: [],
        });
    },
    async handleAdd(e) {
        const { id } = e.currentTarget.dataset;

        var _this = this;
        wx.showModal({
            title: "提示",
            content: "确定要添加吗",
            success: async function (showRes) {
                if (showRes.confirm) {
                    await _this.toAdd(id);
                }
            },
        });
    },
    async toAdd(id) {
        try {
            //弹框选择家庭关系
            const res = await wx.$Api.memberFamily.Add({
                data: {
                    MemberId: id
                },
            });

            this.updateNeedRefresh(true);
            //返回
            wx.navigateBack({
                delta: 1,
            });
        } catch (e) {
            wx.$Fun.showToast(e.Message);
        }
    },
    handlePreview(e) {
        var url = e.currentTarget.dataset.url
        if (url) {
            wx.previewImage({
                urls: [url],
            })
        }
    },
})
);
